package com.xiaoyi.intentsdklibrary.Bean;

import android.content.Context;
import android.text.TextUtils;
import java.util.ArrayList;
import java.util.List;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.query.QueryBuilder;
import org.greenrobot.greendao.query.WhereCondition;

public class ShareBeanSqlUtil {
    private static final ShareBeanSqlUtil ourInstance = new ShareBeanSqlUtil();
    private ShareBeanDao mShareBeanDao;

    public static ShareBeanSqlUtil getInstance() {
        return ourInstance;
    }

    private ShareBeanSqlUtil() {
    }

    public void initDbHelp(Context context) {
        this.mShareBeanDao = new DaoMaster(new DaoMaster.DevOpenHelper(context, "intentsdk-db", null).getWritableDatabase()).newSession().getShareBeanDao();
    }

    public void add(ShareBean shareBean) {
        this.mShareBeanDao.insertOrReplace(shareBean);
    }

    public void addList(List<ShareBean> list) {
        this.mShareBeanDao.insertOrReplaceInTx(list);
    }

    public void update(ShareBean shareBean) {
        this.mShareBeanDao.update(shareBean);
    }

    public void delAll() {
        try {
            this.mShareBeanDao.deleteInTx(this.mShareBeanDao.queryBuilder().build().list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delAllByType(String str) {
        if (str.equals("top")) {
            delAll();
            return;
        }
        try {
            this.mShareBeanDao.deleteInTx(this.mShareBeanDao.queryBuilder().where(ShareBeanDao.Properties.Script_type_code.eq(str), new WhereCondition[0]).build().list());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void delByType(String str) {
        if (str == null) {
            str = "";
        }
        ArrayList arrayList = (ArrayList) this.mShareBeanDao.queryBuilder().where(ShareBeanDao.Properties.Script_type_code.eq(str), new WhereCondition[0]).list();
        if (arrayList.size() > 0) {
            this.mShareBeanDao.deleteInTx(arrayList);
        }
    }

    public ShareBean searchByID(String str) {
        new ArrayList();
        if (str == null) {
            str = "";
        }
        try {
            ArrayList arrayList = (ArrayList) this.mShareBeanDao.queryBuilder().where(ShareBeanDao.Properties.Script_id.eq(str), new WhereCondition[0]).orderDesc(ShareBeanDao.Properties.Down_sum).list();
            if (arrayList == null || arrayList.size() <= 0) {
                return null;
            }
            return (ShareBean) arrayList.get(0);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public List<ShareBean> searchByType(String str) {
        if (TextUtils.isEmpty(str)) {
            str = "0";
        }
        if (str.equals("top")) {
            return searchTop();
        }
        ArrayList arrayList = (ArrayList) this.mShareBeanDao.queryBuilder().where(ShareBeanDao.Properties.Script_type_code.eq(str), new WhereCondition[0]).orderDesc(ShareBeanDao.Properties.Down_sum).list();
        return arrayList == null ? new ArrayList() : arrayList;
    }

    public List<ShareBean> searchByName(String str) {
        QueryBuilder queryBuilder = this.mShareBeanDao.queryBuilder();
        Property property = ShareBeanDao.Properties.Script_name;
        ArrayList arrayList = (ArrayList) queryBuilder.where(property.like("%" + str + "%"), new WhereCondition[0]).orderDesc(ShareBeanDao.Properties.Down_sum).list();
        return arrayList == null ? new ArrayList() : arrayList;
    }

    public List<ShareBean> searchAll() {
        List<ShareBean> list = this.mShareBeanDao.queryBuilder().orderDesc(ShareBeanDao.Properties.Down_sum).build().list();
        return list == null ? new ArrayList() : list;
    }

    public List<ShareBean> searchTop() {
        List<ShareBean> searchAll = searchAll();
        if (searchAll.size() <= 10) {
            return searchAll;
        }
        return searchAll.subList(0, 9);
    }
}
